package com.example.waterorder.controller;

import com.example.waterorder.entity.User;
import com.example.waterorder.tools.Result;
import com.example.waterorder.entity.constant.WaterMessage;
import com.example.waterorder.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/select")
    @Operation(summary = "通过用户id查询用户信息")
    public Result selectById(@RequestParam("userId") String userId) {
        User user = userService.selectById(userId);
        return user != null ? new Result(true, user)
                : new Result(false);
    }

    @PostMapping("/register")
    @Operation(summary = "用户注册,返回消息提示")
    public Result register(@RequestBody User user) {
        Integer flag = userService.register(user);
        return flag == 1 ? new Result(true, WaterMessage.REGISTER_SUCCESS)
                : new Result(false, WaterMessage.REGISTER_FAILD);
    }

    @PutMapping("/update")
    @Operation(summary = "用户更改个人信息")
    public Result updateUser(@RequestBody User user) {
        Integer flag = userService.updateUser(user);
        return flag == 1 ? new Result(true, WaterMessage.UPDATE_SUCCESS)
                : new Result(false, WaterMessage.UPDATE_FAILD);
    }
}
